package com.qijian.flowable.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.SecurityUtils;
import com.qijian.common.utils.StringUtils;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.flowable.constant.FlowableConstant;
import com.qijian.flowable.domain.FlowableRecord;
import com.qijian.flowable.domain.query.FlowableRecordQuery;
import com.qijian.flowable.service.IFlowableRecordService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程记录Controller
 *
 * @author qijian
 * @date 2021-09-17
 */
@Api(tags = "流程模块：流程记录")
@RestController
@RequiredArgsConstructor
@RequestMapping("/flowable/flowableRecord")
public class FlowableRecordController extends BaseController {

    private final IFlowableRecordService flowableRecordService;

    /**
     * 查询流程记录列表
     */
    @GetMapping(value = "/list")
    public TableDataInfo list(FlowableRecordQuery query, IPage<FlowableRecord> page) {
        query.setUserIdALL(SecurityUtils.getUserId());
        query.setPowerType(FlowableConstant.WC);
        if (StringUtils.isNotBlank(query.getPowerType()) && query.getPowerType().equals(FlowableConstant.CC)) {
            query.setUserIdALL(null);
            query.setPowerUserId(SecurityUtils.getUserId());
        }
        return getDataTable(flowableRecordService.pageVoNew(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query,false), query));
    }

    @GetMapping(value = "/my")
    public TableDataInfo my(FlowableRecordQuery query, IPage<FlowableRecord> page) {
        query.setUserId(SecurityUtils.getUserId());
        if (CollectionUtil.isEmpty(query.getCreateTime())) {
            query.setCreateTime(null);
        }
        return getDataTable(flowableRecordService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    /**
     * 导出流程记录列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:flowableRecord:export')")
    @Log(title = "流程记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FlowableRecordQuery query) {
        return new ExcelUtil<>(FlowableRecord.class).exportExcel(flowableRecordService.list(WhereEntityTool.invoke(query)), "流程记录数据");
    }

    /**
     * 获取流程记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('flowable:flowableRecord:query')")
    @GetMapping(value = "/{processInstanceId}")
    public AjaxResult getInfo(@PathVariable("processInstanceId") String processInstanceId) {
        return AjaxResult.success(flowableRecordService.getById(processInstanceId));
    }

    /**
     * 新增流程记录
     */
    @PreAuthorize("@ss.hasPermi('flowable:flowableRecord:add')")
    @Log(title = "流程记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FlowableRecord flowableRecord) {
        return toAjax(flowableRecordService.save(flowableRecord));
    }

    /**
     * 修改流程记录
     */
    @PreAuthorize("@ss.hasPermi('flowable:flowableRecord:edit')")
    @Log(title = "流程记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FlowableRecord flowableRecord) {
        return toAjax(flowableRecordService.updateById(flowableRecord));
    }

    /**
     * 删除流程记录
     */
    @PreAuthorize("@ss.hasPermi('flowable:flowableRecord:remove')")
    @Log(title = "流程记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{processInstanceIds}")
    public AjaxResult remove(@PathVariable List<String> processInstanceIds) {
        return toAjax(flowableRecordService.removeByIds(processInstanceIds));
    }
}
